package com.wjm.shootgame.shoot.img;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
/*
* 图片工具类
* */
public class Images {
    //对于图片数量布置一张的可以设置为为数组
    public static BufferedImage[] heroImage;
    public static BufferedImage bulletImage;


    public static BufferedImage[] bigAirplaneImage;
    public static BufferedImage[] airplaneImage;
    public static BufferedImage[] beeImage;


    public static BufferedImage skyImage;
    public static BufferedImage startImage;
    public static BufferedImage purseImage;
    public static BufferedImage gameOverImage;
    public static BufferedImage myImage;

    //静态块的方式初始化属性
    static {
        heroImage = new BufferedImage[2];
        heroImage[0] = readImage("hero0.png");
        heroImage[1] = readImage("hero1.png");

        bigAirplaneImage = new BufferedImage[5];
        airplaneImage = new BufferedImage[5];
        beeImage = new BufferedImage[5];
        bigAirplaneImage[0] = readImage("bigairplane.png");
        airplaneImage[0] = readImage("airplane.png");
        beeImage[0] = readImage("bee.png");
        //为敌机读取爆炸图片
        for (int i = 1;i<5;i++){
            bigAirplaneImage[i] = readImage("bom"+i+".png");
            airplaneImage[i] = readImage("bom"+i+".png");
            beeImage[i] = readImage("bom"+i+".png");
        }

        bulletImage = readImage("bullet.png");
        skyImage = readImage("background.png");
        startImage = readImage("start.png");
        purseImage = readImage("purse.png");
        gameOverImage = readImage("gameover.png");
        myImage = readImage("my.png");

    }

    /*
     * java中异常处理
     * 1、检查异常：编译器进行了代码检查，需要做必要处理才行
     * 2、运行异常：只有在程序运行时才会捕捉到的异常，比如NullPointException(空指针异常)
     * 检查异常的两种处理方式：1）向调用者抛出可能出现的异常，最后一级一级往上抛，最后抛给jvm（throw）
     *                    2）自己进行捕获处理，对调用者不可见，就是将捕获的异常打印出栈（try...catch...）
     *
     * */
    /**
     * 对读取图片的代码进行重载
     * @param imageName 需要读取的图片名称
     * @return image 放回读取的图片
     */
    public static BufferedImage readImage(String imageName){
        BufferedImage image = null;
        try {
             //使用的方式是加载url
             image = ImageIO.read(Images.class.getResource(imageName));

        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }
}
